<template>
  <div class="app-container">
    <!-- 搜索相关区域 -->
    <div class="filter-container">
      <el-form :inline="true" :model="searchParam" class="demo-form-inline">
        <el-form-item label="手机号">
          <el-input
            v-model="searchParam.phone"
            size="mini"
            placeholder="请输入手机号"
            clearable
          />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchParam.status" size="mini" placeholder="请选择" clearable>
            <el-option label="待审核" value="0"/>
            <el-option label="审核通过" value="1"/>
            <el-option label="审核驳回" value="2"/>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="mini"
            icon="el-icon-search"
            @click="getPage()"
          >查询
          </el-button>
          <el-button
            size="mini"
            icon="el-icon-refresh"
            @click="() => {this.searchParam = {}, this.pageQuery = {pageSize: 10,pageNum: 1}, this.getPage()}"
          >重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 列表相关区域 -->
    <el-table
      v-loading="pageLoading"
      :data="pageVO.list"
      border
      fit
      highlight-current-row
      @selection-change="handleSelectionChange"
      style="width: 100%;"
    >
      <el-table-column
        type="selection"
        align="center"
        width="60">
      </el-table-column>
      <el-table-column label="手机号" prop="phone" align="center">
        <template slot-scope="{row}">
          <span>{{ row.phone }}</span>
        </template>
      </el-table-column>
      <el-table-column label="身份证号" prop="idNum" align="center">
        <template slot-scope="{row}">
          <span>{{ row.idNum }}</span>
        </template>
      </el-table-column>
      <el-table-column label="划转数量" prop="num" align="center" width="100">
        <template slot-scope="{row}">
          <span>{{ row.num }}</span>
        </template>
      </el-table-column>
      <el-table-column label="状态" prop="status" align="center" width="120">
        <template slot-scope="{row}">
          <el-tag v-if="row.status === '0'" size="small" type="warning">待审核</el-tag>
          <el-tag v-if="row.status === '1'" size="small" type="warning">审核通过</el-tag>
          <el-tag v-if="row.status === '2'" size="small" type="danger">审核驳回</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" prop="createTime" align="center" width="160">
        <template slot-scope="{row}">
          <span>{{ row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" width="150" class-name="small-padding fixed-width"
                       fixed="right">
        <template slot-scope="{row}">
          <el-button
            v-if=" row.status === 0"
            type="primary"
            size="mini"
            plain
            round
            @click="enableOrDisable(row,1)"
          >通过
          </el-button>
          <el-button
            v-if=" row.status === 0"
            type="danger"
            size="mini"
            plain
            round
            @click="enableOrDisable(row,2)"
          >驳回
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页条 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pageQuery.pageNum"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageQuery.pageSize"
      :total="pageVO.total"
      layout="total, sizes, prev, pager, next, jumper"
    ></el-pagination>

  </div>
</template>

<script>
export default {
  name: '',
  components: {},
  data () {
    return {
      resourcesUrl: process.env.VUE_APP_RESOURCES_URL,
      // 查询的参数
      pageQuery: {
        pageSize: 10,
        pageNum: 1
      },
      // 返回参数
      pageVO: {
        list: [], // 返回的列表
        total: 0, // 一共多少条数据
        pages: 0 // 一共多少页
      },
      // loading
      pageLoading: true,
      // 查询参数
      searchParam: {},
      // addOrUpdateVisible: false,
      consignListVisible: false,
      auditRecordVisible: false,
      selected: [],
      dialogVisible: false,
      reason: null
    }
  },
  mounted () {
    this.getPage()
  },
  methods: {
    handleSizeChange (val) {
      this.pageQuery.pageSize = val
      this.getPage()
    },
    handleCurrentChange (val) {
      this.pageQuery.pageNum = val
      this.getPage()
    },
    getPage () {
      if (this.searchParam.userId) {
        var regPos = /^[0-9]\d*$/
        if (!regPos.test(this.searchParam.userId)) {
          this.$message({
            message: '用户ID只可输入数字',
            type: 'error',
            duration: 1500
          })
          return
        }
      }
      this.pageLoading = true
      this.$http({
        url: this.$http.adornUrl('/transferRecord'),
        method: 'get',
        params: this.$http.adornParams(
          Object.assign({...this.pageQuery, ...this.searchParam}
          )
        )
      }).then(({data}) => {
        this.pageLoading = false
        this.pageVO.list = data
        this.pageVO.total = data.total
      })
    },
    handleClose () {
      this.reason = null
      this.dialogVisible = false
    },
    handleSelectionChange (val) {
      this.selected = val.map(item => item.id)
    },
    /**
     * 驳回/通过
     */
    enableOrDisable (row, sts) {
      var tit = '请输入' + (sts === 2 ? '驳回' : '通过') + '原因'
      this.$prompt(tit, '寄售审核', {
        confirmButtonText: '确定',
        cancelButtonText: '取消'
      }).then(({value}) => {
        if (sts === 2 && !value) {
          this.$message({
            message: '请填写驳回原因！',
            type: 'error',
            duration: 1200
          })
          return
        }
        var data = {}
        data.id = row.id
        data.status = sts
        data.message = value
        data.phone = row.phone
        data.idNum = row.idNum
        this.$http({
          url: this.$http.adornUrl('/transferRecord'),
          method: 'put',
          data: this.$http.adornData(data)
        }).then(async ({ data }) => {
          this.$message({
            type: 'success',
            message: (sts === 2 ? '驳回' : '通过') + '成功',
            duration: 1500,
            onClose: () => this.getPage()
          })
        })
      }).catch(() => {
      })
    }

  }
}
</script>

<style lang="scss" scoped>
.app-container {
  .brand-logo-img {
    width: auto;
    height: auto;
    max-width: 100%;
    max-height: 200px;
  }
}
</style>
